package interview;

import java.util.ArrayList;
import java.util.LinkedList;

public class interview_03_03 {
    class StackOfPlates {

        private ArrayList<int[]> list;
        int size;
        int cap;
        public StackOfPlates(int cap) {
            list=new ArrayList<>();
            size=-1;
            this.cap=cap;
        }

        public void push(int val) {
            if(size==-1){
                list.add(new int[cap+1]);
                size++;
                list.get(size)[0]=2;
                if(cap>=1) list.get(size)[1]=val;
                else list.get(size)[0]=1;
            }
            else if(list.get(size)[0]==cap+1){
                size++;
                list.add(new int[cap+1]);
                list.get(size)[0]=2;
                if(cap>=1) list.get(size)[1]=val;
                else list.get(size)[0]=1;
            }
            else{
                if(cap==0) return;
                int index=list.get(size)[0];
                list.get(size)[index]=val;
                list.get(size)[0]+=1;
            }
        }

        public int pop() {
            if(size==-1) return -1;
            if(list.get(size)[0]==1) return -1;
            int index=list.get(size)[0]--;
            int result=list.get(size)[index-1];
            if(index==2){

                size--;
            }
            return result;
        }

        public int popAt(int index) {
            if(index>size) return -1;
            if(list.get(index)==null) return -1;
            if(list.get(index)[0]==1) return -1;
            int tmp=list.get(index)[0]--;
            int result=list.get(index)[tmp-1];
            if(tmp==2){
                list.remove(index);
                size--;
            }
            return result;
        }
    }
}
